<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageFeedback;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\FeedbackType;
use support\Request;
use think\facade\Db;

class FeedbackController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('uid', 'input', 'UID', [
            'placeholder' => 'UID搜索'
        ]);
        $builder->addScreen('mobile', 'input', '手机号', [
            'placeholder' => '手机号搜索'
        ]);
        $builder->addScreen('content', 'input', '内容', [
            'placeholder' => '内容搜索'
        ]);
        $builder->addScreen('state', 'select', '状态', [
            'options' => Examine::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addRightButton('edit', '编辑', [
            'api'    => $this->pluginPrefix . '/admin/Feedback/edit',
            'path' => '/Feedback/edit',
            'type'      => 'modal'
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addRightButton('del', '删除', [
            'type'   => 'confirm',
            'api'    => $this->pluginPrefix . '/admin/Feedback/delete',
            'method' => 'delete'
        ], [
            'title'   => '温馨提示',
            'content' => '是否确认删除该数据',
        ], [
            'type' => 'error',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120,
            ])
            ->addColumnEle('feedback_type', '类型', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => FeedbackType::dictOptions(),
                ],
            ])
            ->addColumn('content', '内容')
            ->addColumn('mobile', '手机号', [
                'width' => 120,
            ])
            ->addColumn('remarks', '备注')
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'switch',
                    'api' => $this->pluginPrefix . '/admin/Feedback/updateState',
                    'checked' => Examine::YES,
                    'unchecked' => Examine::WAIT
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['f.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['f.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if ($mobile) {
            $where[] = ['f.mobile', 'like', "%{$mobile}%"];
        }
        $content = $request->get('content');
        if ($content) {
            $where[] = ['f.content', 'like', "%{$content}%"];
        }
        $state = $request->get('state');
        if ($state !== null) {
            $where[] = ['f.state', '=', $state];
        }
        $data = MassageFeedback::where($where)
            ->alias('f')
            ->join('MassageUser u', 'u.id = f.uid', 'left')
            ->order('f.create_time desc')
            ->field('f.*,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                if ($item->uid) {
                    if ($item->nickname) {
                        $item->nickname = base64_decode($item->nickname);
                    }
                    if (strpos($item->headimg, 'http') !== 0) {
                        $item->headimg = UploadService::url($item->headimg);
                    }
                }
                if ($item->state === Examine::NO['value']) {
                    $item->state = Examine::WAIT['value'];
                }
            });
        return $this->successRes($data);
    }
    public function edit(Request $request)
    {
        $id = $request->get('id');
        $MassageFeedback = MassageFeedback::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageFeedback) {
            return $this->fail('数据不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            $MassageFeedback->state = $D['state'];
            Db::startTrans();
            try {
                $MassageFeedback->remarks = $D['remarks'];
                $MassageFeedback->save();
                Db::commit();
                return $this->success('审核成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('state', 'radio', '状态', Examine::YES['value'], [
            'col' => 24,
            'options' => Examine::getOptions()
        ]);
        $builder->addRow('content', 'textarea', '内容', '', [
            'col' => 24,
            'props' => [
                'disabled' => true
            ]
        ]);
        $builder->addRow('remarks', 'textarea', '备注', '', [
            'col' => 24
        ]);
        $data = $builder->setData($MassageFeedback)->create();
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $id = $request->post('id');
        $MassageFeedback = MassageFeedback::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        $MassageFeedback->state = $MassageFeedback->state == Examine::YES['value'] ? Examine::NO['value'] : Examine::YES['value'];
        if ($MassageFeedback->save()) {
            return $this->success('修改成功');
        }
        return $this->fail('修改失败');
    }
    public function delete(Request $request)
    {
        $id = $request->post('id');
        $where = [
            ['saas_appid', '=', $request->saas_appid],
            ['id', '=', $id]
        ];
        $MassageFeedback = MassageFeedback::where($where)->find();
        if (!$MassageFeedback) {
            return $this->fail('数据不存在');
        }
        if ($MassageFeedback->delete()) {
            return $this->success('删除成功');
        }
        return $this->fail('删除失败');
    }
}
